$(function(){
	var currentForm;

	$( '#msgDialog' ).dialog({
		draggable:false,
		autoOpen: false,
		resizable: false,
		maxWidth:600,
		maxHeight: 500,
		width: 400,
		height: 360,
		modal: true,
		buttons: {
			'No, I want to change something.' : function()
			{
				$(this).dialog('close');
			},
			'Yes. Everything is correct, proceed.': function() {
				$(window).unbind('beforeunload');
				currentForm.submit();
				//console.log(currentForm);
			}
		}
	});
	
	$( '#msgDialog2' ).dialog({
		draggable:false,
		autoOpen: false,
		resizable: false,
		maxWidth:600,
		maxHeight: 500,
		width: 400,
		height: 360,
		modal: true,
		buttons: {
			'Close.' : function()
			{
				$(this).dialog('close');
			}
		}
	});
	
	// check grade level form
	$('#check-level-form').submit(function (event) {	
		event.preventDefault();
		currentForm = this;
		$(this).on('valid', function (event) {
			var grade = $('#level-section option:selected').text();
			var block = $('input[name=section]:checked', '#check-level-form').attr('title');
			var thehtml ='';
			var block_f;
			
			if(typeof block == 'undefined')
			{
				block_f = 'No Block Set for '+ grade +'. Please Proceed.';
			}else{
				block_f = block;
			}
			thehtml += '<p>Selected grade level is: <strong>'+ grade +'</strong></p>';
			thehtml += '<p>Selected Block is: <strong>'+ block_f +'</strong></p>';
			$( '.ui-dialog-title' ).html('Grade Level and Block.');
			$( '#msgDialog > div.content' ).html(thehtml);
			$( '#msgDialog' ).dialog('open');
		});	
	});
	
	$('#check-form-submit').submit(function (event) {
		event.preventDefault();
		currentForm = this;
		//console.log(currentForm);	
	});
	
	$('#check-form-submit').on('invalid', function (event) {
		$( '.ui-dialog-title' ).html('Error.');
		$( '#msgDialog2 > div.content' ).html('<p><strong>Please fix the fields marked with red.</strong></p>');
		$( '#msgDialog2' ).dialog('open');
	});
		
	$('#check-form-submit').on('valid', function (event) {
		$( '.ui-dialog-title' ).html('Verify Registration Form.');
		$( '#msgDialog > div.content' ).html('<p><strong>Please confirm that all data is true and correct.</strong></p>');
		$( '#msgDialog' ).dialog('open');
	});
});